package com.pc.cloud.utils;

import org.apache.log4j.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.NoInitialContextException;
import java.io.File;

public class IndexHomeUtils {
	private static final Logger log = Logger.getLogger(IndexHomeUtils.class);

    public static String getIndexHome(){
        String project = "tas";
		String home = null;

		// Try JNDI
	    try {
	      Context c = new InitialContext();
	      home = (String)c.lookup("java:comp/env/"+project+"/home");
	      log.info("Using JNDI solr.home: "+home );
	    } catch (NoInitialContextException e) {
	      log.info("JNDI not configured for "+project+" (NoInitialContextEx)");
	    } catch (NamingException e) {
	      log.info("No /"+project+"/home in JNDI");
	    } catch( RuntimeException ex ) {
	      log.warn("Odd RuntimeException while testing for JNDI: " + ex.getMessage());
	    }

		// Now try system property
	    if( home == null ) {
	      String prop = project + "tas.home";
	      home = System.getProperty(prop);
	      if( home != null ) {
	        log.info("using system property "+prop+": " + home );
	      }
	    }
	    //
	    if(home == null) {
	    	home = System.getProperty("java.io.tmpdir") + File.separator;
	    }
        return home;
    }


	public static String getIndexerDirectory() {
        String home = getIndexHome();
        if(home != null){
            home = home + "tas" + File.separator + "indexer" + File.separator;
        }
		return home;
	}

    //for lucene uncommit file
    public static String getPenguinCatalog() {
        String home = getIndexHome();
        if (home != null) {
            home = home + "tas" + File.separator + "catalog";
        }
		return home;
	}

}
